
// gcc cjson/cJSON.c test.c -lm

#include <stdio.h>
#include <stdlib.h>
#include "cjson/cJSON.h"
#include <string.h>

/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
{
	printf("----> %s\n", __func__);

	// printf("----> %s\n",text);
	char *out;cJSON *json;

	cJSON *syslog_obj = NULL;
	cJSON *cert_obj = NULL;
	cJSON *root = NULL;
	char *content = NULL;
	char *cur = NULL;
	cJSON *ret = NULL;
	cJSON *server_obj = NULL;
	cJSON *server_cert_obj = NULL;
	cJSON *server_cert_cert_obj = NULL;
	int server_secure = 0;

	syslog_obj=cJSON_Parse(text);

	cur = cJSON_GetStringFromObject(syslog_obj, "log_ip");

	printf("sysinfo.log_ip  is [%s]\n", cur);
	if (cur && strlen(cur) != 0) {
		printf("sysinfo.log_ip  is [%s]\n", cur);
	}
	char buf[16] = {0};
	if ((ret = cJSON_GetObjectItem(syslog_obj, "log_remote"))) {
		sprintf(buf, "%d", ret->valueint);
		printf("sysinfo.log_remote is [%s]\n", buf);
	}
	ret = cJSON_GetObjectItem(syslog_obj, "log_port");
	if (ret) {
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "%d", ret->valueint);
		printf("sysinfo.log_port is [%d]\n", ret->valueint);
	}

	cur = cJSON_GetStringFromObject(syslog_obj, "log_priority");
	if (cur && strlen(cur) != 0) {
		printf("sysinfo.log_priority is [%s],size:[%ld]\n", cur, strlen(cur));
	}

	if ((ret = cJSON_GetObjectItem(syslog_obj, "log_secure"))) {
		sprintf(buf, "%d", ret->valueint);
		printf("sysinfo.log_secure is [%s]\n", buf);
	}

	printf("----> debug1\n");
	char *cert_name = NULL;
	char *cert_md5 = NULL;
	char *down_url = NULL;
	if (strcmp(buf, "1") == 0) {
		cert_obj = cJSON_GetObjectItem(syslog_obj, "log_cert");
		if (cert_obj) {
			cert_name = cJSON_GetStringFromObject(cert_obj, "cert_name");
			cert_md5 = cJSON_GetStringFromObject(cert_obj, "cert_md5");
			down_url = cJSON_GetStringFromObject(cert_obj, "down_url");
		}

		printf("cert_name[%s], cert_md5[%s], url[%s]\n",cert_name, cert_md5, down_url);
		// syslog_down_cert(cert_name, cert_md5, down_url);
	
	}

	server_obj = cJSON_GetObjectItem(syslog_obj, "log_server");
	int size = 0;
	int server_num = 0;

	printf("size is %d\n", size);

	size = cJSON_GetArraySize(server_obj);
	printf("size is %d\n", size);
	for (server_num = 0; server_num < size; server_num++) {
		server_cert_obj = cJSON_GetArrayItem(server_obj, server_num);
		server_secure = cJSON_GetIntFromObject(server_cert_obj, "server_secure");
		printf("server_secure is %d\n", server_secure);

		if (server_secure == 1) {
			server_cert_cert_obj = cJSON_GetObjectItem(server_cert_obj, "server_cert");
			if (server_cert_cert_obj) {
				cert_name = cJSON_GetStringFromObject(server_cert_cert_obj, "cert_name");
				cert_md5 = cJSON_GetStringFromObject(server_cert_cert_obj, "cert_md5");
				down_url = cJSON_GetStringFromObject(server_cert_cert_obj, "down_url");
			}
			printf("%d is cert_name[%s], cert_md5[%s], url[%s]\n",server_num, cert_name, cert_md5, down_url);
			// syslog_down_cert(cert_name, cert_md5, down_url);
		}
	}


	// if (!json) {
	// 	printf("Error before: [%s]\n",cJSON_GetErrorPtr());
	// } else {
	// 	out=cJSON_Print(json);
	// 	cJSON_Delete(json);
	// 	// printf("%s\n",out);
	// 	free(out);
	// }
}

/* Read a file, parse, render back, etc. */
void dofile(char *filename)
{
	printf("----> %s\n", __func__);
	FILE *f;long len;char *data;

	f=fopen(filename,"rb");fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET);
	data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);
	doit(data);
	free(data);
}


int main (int argc, const char * argv[]) 
{
	printf("hello world\n");
	/* Process each json textblock by parsing, then rebuilding: */
	dofile("syslog.conf");
	
	return 0;
}
